package com.gitee.huxing.course.conc.conc02.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.conc02.lock
 * @ClassName: Count
 * @Author: huxingmale
 * @DateTime: 2021-08-24 21:25
 */
public class Count3 {

    private byte[] lock1 = new byte[1];
    private byte[] lock2 = new byte[2];

    public int num = 0;

    public void add(){
        synchronized (lock1){
            try {
                Thread.sleep(1000l);
            } catch (Exception ex){
                ex.printStackTrace();
            }
            synchronized (lock2){
                num += 1;
            }
            System.out.println(Thread.currentThread().getName() + "_" + num);
        }
    }

    public void lockMethod(){
        synchronized (lock2){
            try {
                Thread.sleep(1000L);
            } catch (Exception ex){
                ex.printStackTrace();
            }
            synchronized (lock1){
                num += 1;
            }
            System.out.println(Thread.currentThread().getName() + "_" + num);
        }
    }
}
